<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="员工姓名" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="员工姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openSelectUser"
          v-hasPermi="['system:role:add']"
          >添加员工</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="cancelAll"
          v-hasPermi="['system:role:remove']"
          >批量移除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
          >关闭</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      border
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="员工姓名"
        prop="staffName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="工号"
        prop="staffNum"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="卡号"
        prop="cardNum"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="手机"
        prop="phone"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column> -->
      <el-table-column
        label="入职时间"
        align="center"
        prop="entryTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="cancelStaffGroup(scope.row)"
            v-hasPermi="['system:role:remove']"
            >移除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <select-staff ref="select" :groupId="queryParams.groupId" @ok="handleQuery" />
  </div>
</template>

<script>
import {
  allocatedStaffList,
  staffGroupCancel,
  attendGroupCancelAll,
} from "@/api/business/attendGroup";
import selectStaff from "./selectStaff";

export default {
  name: "AuthUser",
  dicts: ["sys_normal_disable"],
  components: { selectStaff },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中用户组
      staffIds: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: undefined,
        staffName: undefined,
        phone: undefined,
      },
    };
  },
  created() {
    const groupId = this.$route.params && this.$route.params.groupId;
    if (groupId) {
      this.queryParams.groupId = groupId;
      this.getList();
    }
  },
  methods: {
    /** 查询授权用户列表 */
    getList() {
      this.loading = true;
      allocatedStaffList(this.queryParams)
        .then((response) => {
          this.userList = response.data.rows;
          this.total = response.data.total;
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 返回按钮
    handleClose() {
      const obj = { path: "/system/role" };
      this.$tab.closeOpenPage(obj);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.staffIds = selection.map((item) => item.staffId);
      this.multiple = !selection.length;
    },
    /** 打开授权用户表弹窗 */
    openSelectUser() {
      this.$refs.select.show();
    },
    /** 取消授权按钮操作 */
    cancelStaffGroup(row) {
      this.$modal
        .confirm('确认将员工["' + row.staffName + '"]从考勤班组中移除吗？')
        .then(function () {
          return staffGroupCancel({ staffId: row.staffId });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功!");
        })
        .catch(() => {});
    },
    /** 批量取消授权按钮操作 */
    cancelAll(row) {
      const staffIds = this.staffIds.join(",");
      this.$modal
        .confirm("是否从考勤班组中移除选中的员工？")
        .then(function () {
          return attendGroupCancelAll({ groupId: 1, staffIds: staffIds});
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功!");
        })
        .catch(() => {});
    },
  },
};
</script>
